package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.product.client.ProductFeignClient;
import com.atguigu.gmall.product.client.common.result.Result;
import com.atguigu.gmall.product.client.model.product.BaseCategoryView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//首页
@Controller
public class indexController {
    @Autowired
    private ProductFeignClient productFeignClient;

    @GetMapping("/")
    public String index(Model model){

        List<Map> listMap = new ArrayList<>();

        //所有分类
        List<BaseCategoryView> baseCategoryViewList = productFeignClient.getBaseCategoryViewList();
        //根基一级id 进行分组 返回值 map
        Map<Long, List<BaseCategoryView>> baseCategoryViewListMap1 = baseCategoryViewList.stream().
                collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id ));

        //1.前60个数据算一个map
        baseCategoryViewListMap1.forEach((k1,v1) -> {
            //一级分类
            Map categoryMap1 = new HashMap<>();
            categoryMap1.put("index",k1);
            categoryMap1.put("categoryId",k1);
            categoryMap1.put("categoryName",v1.get(0).getCategory1Name());

            //二级分类
            Map<Long, List<BaseCategoryView>> baseCategoryViewListMap2 = v1.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            List<Map> listMap2 = new ArrayList<>();

            baseCategoryViewListMap2.forEach((k2,v2) -> {
                Map categoryMap2 = new HashMap<>();
                categoryMap2.put("index",k2);
                categoryMap2.put("categoryId",k2);
                categoryMap2.put("categoryName",v2.get(0).getCategory1Name());

                //三级分类
                List<Map> listMap3 = new ArrayList<>();
                v2.forEach(baseCategoryView -> {
                    Map categoryMap3 = new HashMap<>();
                    categoryMap3.put("categoryId",baseCategoryView.getCategory3Id());
                    categoryMap3.put("categoryName",baseCategoryView.getCategory3Name());
                    listMap3.add(categoryMap3);
                });

                categoryMap2.put("categoryChild",listMap3);
                listMap2.add(categoryMap2);
            });
            categoryMap1.put("categoryChild",listMap2);
            listMap.add(categoryMap1);
        });

        model.addAttribute("list",listMap);

        //首页路径
        return "index/index";
    }

    @Autowired
    private TemplateEngine templateEngine;
    //使用静态话程序生成首页   保存在指定的盘符下  盘符挂载到nginx服务上去
    @GetMapping("/createHtml")
    @ResponseBody
    public Result createHtml() throws IOException {
        //首页生成  数据+模板 ==输出流 (将页面保存在指定磁盘上)
        //参数1 模板 ,参数2 数据 ,参数3 输出流
        Context context = new Context();
        context.setVariable("list",getData());
        Writer writer = new PrintWriter("D:\\temp\\index.html","utf-8");
        templateEngine.process("index/index",context,writer);

        return  Result.ok();
    }

    public List<Map> getData(){

        List<Map> listMap = new ArrayList<>();

        //所有分类
        List<BaseCategoryView> baseCategoryViewList = productFeignClient.getBaseCategoryViewList();
        //根基一级id 进行分组 返回值 map
        Map<Long, List<BaseCategoryView>> baseCategoryViewListMap1 = baseCategoryViewList.stream().
                collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id ));

        //1.前60个数据算一个map
        baseCategoryViewListMap1.forEach((k1,v1) -> {
            //一级分类
            Map categoryMap1 = new HashMap<>();
            categoryMap1.put("index",k1);
            categoryMap1.put("categoryId",k1);
            categoryMap1.put("categoryName",v1.get(0).getCategory1Name());

            //二级分类
            Map<Long, List<BaseCategoryView>> baseCategoryViewListMap2 = v1.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            List<Map> listMap2 = new ArrayList<>();

            baseCategoryViewListMap2.forEach((k2,v2) -> {
                Map categoryMap2 = new HashMap<>();
                categoryMap2.put("index",k2);
                categoryMap2.put("categoryId",k2);
                categoryMap2.put("categoryName",v2.get(0).getCategory1Name());

                //三级分类
                List<Map> listMap3 = new ArrayList<>();
                v2.forEach(baseCategoryView -> {
                    Map categoryMap3 = new HashMap<>();
                    categoryMap3.put("categoryId",baseCategoryView.getCategory3Id());
                    categoryMap3.put("categoryName",baseCategoryView.getCategory3Name());
                    listMap3.add(categoryMap3);
                });

                categoryMap2.put("categoryChild",listMap3);
                listMap2.add(categoryMap2);
            });
            categoryMap1.put("categoryChild",listMap2);
            listMap.add(categoryMap1);
        });
        return listMap;
    }

}
